__author__ = 'Nick'

import glob
import sys
import os
import re
import time
import rarfile

#speicify path for unrar.exe 
rarfile.UNRAR_TOOL = "C:\\unrar\\unrar.exe"
#passwrod
pwd = "www.licai668.cn"
#path
path = "D:\\DFW\\"

#search for all monthly file
# monthlyfile = glob.glob(path+'*d.rar')
monthlyfile = glob.glob(path+('*')+('[0-9]'*6)+'d.rar')
# p=re.compile(r'(\d+)d.rar')
mstart = time.time()
for f in monthlyfile:
	rstart = time.time()
	# date_index=p.search(f).groups()[0]
	a=rarfile.RarFile(f)
	a.extractall(path=path+"Level2Data\\", pwd=pwd)
	os.rename(f, f.replace(path, path+'BAK\\'))
	sys.stdout.write("\rextracted %s in %.2f seconds" % (f, (time.time() - rstart)))
	sys.stdout.flush()
print("\nextracted all monthly RAR file in %.2f minutes\n" % (time.time() - mstart))

#daily file
#search for all daily rar file
dailyfile = glob.glob(path+"Level2Data\\"+'*.rar')
#file pattern
p=re.compile(r'\w{2}\_(\d+).rar')
dstart = time.time()
for f in dailyfile:
	rstart = time.time()
	date_index=p.search(f).groups()[0]
	a=rarfile.RarFile(f)
	a.extractall(path=path+"Level2Data\\"+str(date_index)+"\\", pwd=pwd)
	os.remove(f)
	sys.stdout.write("\rDate: %s in %.2f seconds" % (str(date_index), (time.time() - rstart)))
	sys.stdout.flush()
print("\nextracted all daily RAR file in %.2f minutes" % (time.time() - dstart)/60)
# a.extractall()